home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayTmr.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-13  |  9.1 KB  |  309 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayTmr 
  4.    Caption         =   "PlayTmr"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.Timer tmrFrame 
  15.       Enabled         =   0   'False
  16.       Interval        =   100
  17.       Left            =   120
  18.       Top             =   1920
  19.    End
  20.    Begin VB.TextBox txtNumFrames 
  21.       Height          =   285
  22.       Left            =   1560
  23.       TabIndex        =   10
  24.       Text            =   "100"
  25.       Top             =   120
  26.       Width           =   375
  27.    End
  28.    Begin VB.OptionButton optRunType 
  29.       Caption         =   "Looping"
  30.       Height          =   255
  31.       Index           =   2
  32.       Left            =   360
  33.       TabIndex        =   8
  34.       Top             =   1560
  35.       Width           =   1095
  36.    End
  37.    Begin VB.OptionButton optRunType 
  38.       Caption         =   "Reversing"
  39.       Height          =   255
  40.       Index           =   1
  41.       Left            =   360
  42.       TabIndex        =   7
  43.       Top             =   1200
  44.       Width           =   1095
  45.    End
  46.    Begin VB.OptionButton optRunType 
  47.       Caption         =   "One time"
  48.       Height          =   255
  49.       Index           =   0
  50.       Left            =   360
  51.       TabIndex        =   6
  52.       Top             =   840
  53.       Value           =   -1  'True
  54.       Width           =   1095
  55.    End
  56.    Begin VB.TextBox txtFramesPerSecond 
  57.       Height          =   285
  58.       Left            =   1560
  59.       TabIndex        =   5
  60.       Text            =   "20"
  61.       Top             =   480
  62.       Width           =   375
  63.    End
  64.    Begin VB.PictureBox picFrame 
  65.       AutoRedraw      =   -1  'True
  66.       AutoSize        =   -1  'True
  67.       Height          =   375
  68.       Index           =   0
  69.       Left            =   1560
  70.       ScaleHeight     =   21
  71.       ScaleMode       =   3  'Pixel
  72.       ScaleWidth      =   21
  73.       TabIndex        =   2
  74.       Top             =   1560
  75.       Visible         =   0   'False
  76.       Width           =   375
  77.    End
  78.    Begin VB.CommandButton cmdStart 
  79.       Caption         =   "Start"
  80.       Default         =   -1  'True
  81.       Enabled         =   0   'False
  82.       Height          =   375
  83.       Left            =   600
  84.       TabIndex        =   1
  85.       Top             =   2040
  86.       Width           =   855
  87.    End
  88.    Begin VB.PictureBox picCanvas 
  89.       Height          =   3810
  90.       Left            =   2040
  91.       ScaleHeight     =   250
  92.       ScaleMode       =   3  'Pixel
  93.       ScaleWidth      =   250
  94.       TabIndex        =   0
  95.       Top             =   0
  96.       Width           =   3810
  97.    End
  98.    Begin MSComDlg.CommonDialog dlgOpenFile 
  99.       Left            =   1560
  100.       Top             =   960
  101.       _ExtentX        =   847
  102.       _ExtentY        =   847
  103.       _Version        =   393216
  104.       CancelError     =   -1  'True
  105.    End
  106.    Begin VB.Label Label2 
  107.       Caption         =   "Frames to load:"
  108.       Height          =   255
  109.       Left            =   120
  110.       TabIndex        =   9
  111.       Top             =   120
  112.       Width           =   1455
  113.    End
  114.    Begin VB.Label Label1 
  115.       Caption         =   "Frames per second:"
  116.       Height          =   255
  117.       Index           =   1
  118.       Left            =   120
  119.       TabIndex        =   4
  120.       Top             =   480
  121.       Width           =   1455
  122.    End
  123.    Begin VB.Label lblResults 
  124.       Height          =   615
  125.       Left            =   120
  126.       TabIndex        =   3
  127.       Top             =   2640
  128.       Width           =   1815
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileOpen 
  133.          Caption         =   "&Open..."
  134.          Shortcut        =   ^O
  135.       End
  136.    End
  137. Attribute VB_Name = "frmPlayTmr"
  138. Attribute VB_GlobalNameSpace = False
  139. Attribute VB_Creatable = False
  140. Attribute VB_PredeclaredId = True
  141. Attribute VB_Exposed = False
  142. Option Explicit
  143. Private Enum RunTypes
  144.     run_OneTime
  145.     run_BackAndForth
  146.     run_Looping
  147. End Enum
  148. Private NumImages As Integer
  149. Private MaxImage As Integer
  150. Private NextImage As Integer
  151. Private RunType As RunTypes
  152. Private RunForward As Integer
  153. Private StartTime As Long
  154. Private StopTime As Long
  155. Private NumPlayed As Integer
  156. ' Load the images.
  157. Private Sub LoadImages(file_name As String)
  158. Dim base As String
  159. Dim i As Integer
  160.     ' Get the base file name.
  161.     base = Left$(file_name, Len(file_name) - 5)
  162.     ' See how many frames the user wants to load.
  163.     If Not IsNumeric(txtNumFrames.Text) Then _
  164.         txtNumFrames.Text = Format$(10)
  165.     NumImages = CInt(txtNumFrames.Text)
  166.     ' Create any needed picture boxes.
  167.     For i = MaxImage + 1 To NumImages - 1
  168.         Load picFrame(i)
  169.     Next i
  170.     ' Get rid of any that are no longer needed.
  171.     For i = NumImages To MaxImage
  172.         Unload picFrame(i)
  173.     Next i
  174.     MaxImage = NumImages - 1
  175.     ' Load the images.
  176.     On Error GoTo LoadPictureError
  177.     i = 0
  178.     Do While i < NumImages
  179.         lblResults.Caption = Format$(i + 1)
  180.         lblResults.Refresh
  181.         picFrame(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  182.         i = i + 1
  183.     Loop
  184.     picCanvas.AutoSize = True
  185.     picCanvas.Picture = picFrame(0).Image
  186.     picCanvas.AutoSize = False
  187.     lblResults.Caption = ""
  188.     txtNumFrames.Text = Format$(NumImages)
  189.     Exit Sub
  190. LoadPictureError:
  191.     ' We ran out of images early.
  192.     NumImages = i
  193.     txtNumFrames.Text = Format$(NumImages)
  194.     Resume Next
  195. End Sub
  196. ' Start or stop playing.
  197. Private Sub CmdStart_Click()
  198.     If tmrFrame.Enabled Then
  199.         ' Stop the animation.
  200.         StopAnimation
  201.     Else
  202.         ' Start the animation.
  203.         StartAnimation
  204.     End If
  205. End Sub
  206. ' Start playing.
  207. Private Sub StartAnimation()
  208. Dim i As Integer
  209.     ' Start the animation.
  210.     cmdStart.Caption = "Stop"
  211.     lblResults.Caption = ""
  212.     NumPlayed = 0
  213.     NextImage = 0
  214.     RunForward = True
  215.     ' See what kind of run it is (looping, etc.).
  216.     For i = 0 To 2
  217.         If optRunType(i).Value Then Exit For
  218.     Next i
  219.     RunType = i
  220.     ' See how long it should be between frames.
  221.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  222.         txtFramesPerSecond.Text = "20"
  223.     tmrFrame.Interval = 1000 / CInt(txtFramesPerSecond.Text)
  224.     ' Start the timer.
  225.     StartTime = GetTickCount
  226.     tmrFrame.Enabled = True
  227. End Sub
  228. ' Stop playing.
  229. Private Sub StopAnimation()
  230. Dim StopTime As Long
  231.     ' Stop the animation.
  232.     StopTime = GetTickCount
  233.     tmrFrame.Enabled = False
  234.     cmdStart.Caption = "Start"
  235.     lblResults.Caption = _
  236.         Format$(NumPlayed) & " frames/" & _
  237.         Format$((StopTime - StartTime) / 1000#, "0.00") & _
  238.         " sec" & vbCrLf & vbCrLf & _
  239.         Format$(CSng(NumPlayed) / ((StopTime - StartTime) / 1000#), "0.00") & _
  240.         " frames/sec"
  241. End Sub
  242. Private Sub Form_Load()
  243.     dlgOpenFile.InitDir = App.Path
  244. End Sub
  245. ' Load new image files.
  246. Private Sub mnuFileOpen_Click()
  247. Dim file_name As String
  248.     ' Let the user select a file.
  249.     On Error Resume Next
  250.     dlgOpenFile.FileName = "*_0.BMP"
  251.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  252.     dlgOpenFile.ShowOpen
  253.     If Err.Number = cdlCancel Then
  254.         Exit Sub
  255.     ElseIf Err.Number <> 0 Then
  256.         Beep
  257.         MsgBox "Error selecting file.", , vbExclamation
  258.         Exit Sub
  259.     End If
  260.     On Error GoTo 0
  261.     Screen.MousePointer = vbHourglass
  262.     DoEvents
  263.     file_name = Trim$(dlgOpenFile.FileName)
  264.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  265.         - Len(dlgOpenFile.FileTitle) - 1)
  266.     Caption = "PlayTmr [" & dlgOpenFile.FileTitle & "]"
  267.     ' Load the pictures.
  268.     On Error GoTo LoadError
  269.     LoadImages file_name
  270.     On Error GoTo 0
  271.     cmdStart.Enabled = True
  272.     Screen.MousePointer = vbDefault
  273.     Exit Sub
  274. LoadError:
  275.     Screen.MousePointer = vbDefault
  276.     MsgBox "Error " & Format$(Err.Number) & _
  277.         " opening file '" & file_name & "'" & vbCrLf & _
  278.         Err.Description
  279. End Sub
  280. ' Display the next frame.
  281. Private Sub tmrFrame_Timer()
  282.     ' Display the next frame.
  283.     picCanvas.Picture = picFrame(NextImage).Image
  284.     NumPlayed = NumPlayed + 1
  285.     ' See which frame comes next.
  286.     If RunForward Then
  287.         ' Display the next frame next.
  288.         NextImage = NextImage + 1
  289.         If NextImage >= NumImages Then
  290.             Select Case RunType
  291.                 Case run_OneTime
  292.                     StopAnimation
  293.                 Case run_BackAndForth
  294.                     RunForward = False
  295.                     NextImage = NumImages - 2
  296.                 Case run_Looping
  297.                     NextImage = 0
  298.             End Select
  299.         End If
  300.     Else
  301.         ' Display the previous frame next.
  302.         NextImage = NextImage - 1
  303.         If NextImage < 0 Then
  304.             RunForward = True
  305.             NextImage = 1
  306.         End If
  307.     End If
  308. End Sub
  309.